#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""

"""
from Base import Base
from docx import Document
from QuestionStore import QuestionStore
import os


class PoetrysToFile(Base):
    def __init__(self):
        super(PoetrysToFile, self).__init__()
        self.Question_store = QuestionStore.instance()

    # 打印诗词列表
    def poetrys_to_word(self):
        document = Document()
        i = 1
        for poetry in self.Question_store.poetry_store:
            if self.cfg.getboolean('to_word', 'poetrys_all'):
                poetry_content = self.Question_store.poetry_store[poetry].get_content()
            else:
                poetry_content = poetry + "  （" + self.Question_store.poetry_store[poetry].sentence[1][:-1] + "）"
            document.add_paragraph(str(i) + ". " + poetry_content)
            i = i + 1
        filename = "诗词列表_" + self.now + '.doc'
        document.save(self.cfg['path']['word_path'] + filename)
        self.run_info('生成诗词汇总到' + self.cfg['path']['word_path'] + filename)
        return filename

    # 生成诗词问卷
    def question_to_word(self):
        document_question = Document()
        document_answer = Document()
        qs = []
        for i in range(1, self.cfg.getint('to_word', 'questions_count') + 1):
            qs.append(self.Question_store.get_question())
        # document_question.add_heading('问题', 0)
        j = 1
        for item in qs:
            document_question.add_heading('问题 ' + str(j), level=1)
            document_question.add_paragraph(item["question"].replace("□", " __ "))
            j = j + 1
        document_question.add_page_break()
        # document_question.add_heading("答案", 0)
        m = 1
        for item in qs:
            document_answer.add_heading('答案' + str(m), level=1)
            document_answer.add_paragraph(item["answer"])
            if item['subject'] == 'question':
                document_answer.add_paragraph(item["explain"])
            m = m + 1
        name_now = self.now
        filename_question = '来自诗词大会的挑战_' + name_now + '.doc'
        document_question.save(self.cfg['path']['word_path'] + filename_question)
        self.run_info('生成问题文件： ' + self.cfg['path']['word_path'] + filename_question)
        filename_answer = '来自诗词大会的答案_' + name_now + '.doc'
        document_answer.save(self.cfg['path']['word_path'] + filename_answer)
        self.run_info('生成答案文件： ' + self.cfg['path']['word_path'] + filename_answer)
        return filename_question + '\n' + filename_answer

    def poetrys_to_txt(self):
        models = ['author', 'authors', 'full_name', 'one']
        model = self.cfg['to_txt']['model']
        self.run_info('输出txt文件的模式为： ' + model)
        if model in models:
            self.def_file_by_path(self.cfg['path']['txt_path'])
            self.run_info('')
        else:
            self.error('输出模式错误！')
        for poetry in self.Question_store.poetry_store:
            poetry_content = ''
            txt_name = ''
            if model == 'author':
                txt_name = self.Question_store.poetry_store[poetry].author + '_' + self.Question_store.poetry_store[poetry].dynasty
                poetry_content = '\n' + self.Question_store.poetry_store[poetry].full_name
            elif model == 'authors':
                authors_list = self.cfg['to_txt']['authors'].split(',')
                if self.Question_store.poetry_store[poetry].author in authors_list:
                    txt_name = self.Question_store.poetry_store[poetry].author
                else:
                    txt_name = '其他'
                poetry_content = '\n' + self.Question_store.poetry_store[poetry].full_name
            elif model == 'full_name':
                txt_name = self.Question_store.poetry_store[poetry].full_name
                poetry_content = self.Question_store.poetry_store[poetry].full_name
            elif model == 'one':
                txt_name = '诗词列表'
                poetry_content = '\n' + self.Question_store.poetry_store[poetry].full_name
            for i in range(1, len(self.Question_store.poetry_store[poetry].sentences) + 1):
                poetry_content = poetry_content + '\n' + self.Question_store.poetry_store[poetry].sentences[i]
            with open(self.cfg['path']['txt_path'] + '\\' + txt_name + '.txt', 'a+') as f:
                f.write(poetry_content)
                self.run_info('生成txt诗词文件：' + self.cfg['path']['txt_path'] + '/' + txt_name + '.txt')

    # 清空文件夹中的文件（不删除其中的文件夹）
    def def_file_by_path(self, path):
        ls = os.listdir(path)
        for file in ls:
            c_path = os.path.join(path, file)
            if not os.path.isdir(c_path):
                os.remove(c_path)


if __name__ == "__main__":
    q = PoetrysToFile()
    file_name = q.poetrys_to_word()
    print(file_name)
